package file_tcp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Server
 * Package: file_tcp
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-14
 * Time: 16:13
 */

public class Server {

    public static void main(String[] args) throws IOException {

        //1.创建ServerSocket
        ServerSocket serverSocket = new ServerSocket(8888);

        //2.接收来自于客户端的socket:accept()
        Socket accept = serverSocket.accept();

        //3.通过Socket获取输出流 接收客户端的发来的数据 将网络中的数据读进来
        InputStream is = accept.getInputStream();

        //4.创建File和FileOutputStream 将文件写到本地
        File file = new File("abc.jpg");
        //将读进来的图片 写到本地
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }

        System.out.println("数据接收完毕");
        fos.close();
        is.close();
        accept.close();
        serverSocket.close();


    }


}
